home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / PEAR / Command / Config.php < prev    next >
PHP Script  |  2004-10-01  |  8KB  |  225 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Stig Bakken <ssb@php.net>                                    |
  17. // |         Tomas V.V.Cox <cox@idecnet.com>                              |
  18. // |                                                                      |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: Config.php,v 1.26 2004/01/08 17:33:13 sniper Exp $
  22.  
  23. require_once "PEAR/Command/Common.php";
  24. require_once "PEAR/Config.php";
  25.  
  26. /**
  27.  * PEAR commands for managing configuration data.
  28.  *
  29.  */
  30. class PEAR_Command_Config extends PEAR_Command_Common
  31. {
  32.     // {{{ properties
  33.  
  34.     var $commands = array(
  35.         'config-show' => array(
  36.             'summary' => 'Show All Settings',
  37.             'function' => 'doConfigShow',
  38.             'shortcut' => 'csh',
  39.             'options' => array(),
  40.             'doc' => '
  41. Displays all configuration values.  An optional argument
  42. may be used to tell which configuration layer to display.  Valid
  43. configuration layers are "user", "system" and "default".
  44. ',
  45.             ),
  46.         'config-get' => array(
  47.             'summary' => 'Show One Setting',
  48.             'function' => 'doConfigGet',
  49.             'shortcut' => 'cg',
  50.             'options' => array(),
  51.             'doc' => '<parameter> [layer]
  52. Displays the value of one configuration parameter.  The
  53. first argument is the name of the parameter, an optional second argument
  54. may be used to tell which configuration layer to look in.  Valid configuration
  55. layers are "user", "system" and "default".  If no layer is specified, a value
  56. will be picked from the first layer that defines the parameter, in the order
  57. just specified.
  58. ',
  59.             ),
  60.         'config-set' => array(
  61.             'summary' => 'Change Setting',
  62.             'function' => 'doConfigSet',
  63.             'shortcut' => 'cs',
  64.             'options' => array(),
  65.             'doc' => '<parameter> <value> [layer]
  66. Sets the value of one configuration parameter.  The first argument is
  67. the name of the parameter, the second argument is the new value.  Some
  68. parameters are subject to validation, and the command will fail with
  69. an error message if the new value does not make sense.  An optional
  70. third argument may be used to specify in which layer to set the
  71. configuration parameter.  The default layer is "user".
  72. ',
  73.             ),
  74.         'config-help' => array(
  75.             'summary' => 'Show Information About Setting',
  76.             'function' => 'doConfigHelp',
  77.             'shortcut' => 'ch',
  78.             'options' => array(),
  79.             'doc' => '[parameter]
  80. Displays help for a configuration parameter.  Without arguments it
  81. displays help for all configuration parameters.
  82. ',
  83.            ),
  84.         );
  85.  
  86.     // }}}
  87.     // {{{ constructor
  88.  
  89.     /**
  90.      * PEAR_Command_Config constructor.
  91.      *
  92.      * @access public
  93.      */
  94.     function PEAR_Command_Config(&$ui, &$config)
  95.     {
  96.         parent::PEAR_Command_Common($ui, $config);
  97.     }
  98.  
  99.     // }}}
  100.  
  101.     // {{{ doConfigShow()
  102.  
  103.     function doConfigShow($command, $options, $params)
  104.     {
  105.         // $params[0] -> the layer
  106.         if ($error = $this->_checkLayer(@$params[0])) {
  107.             return $this->raiseError($error);
  108.         }
  109.         $keys = $this->config->getKeys();
  110.         sort($keys);
  111.         $data = array('caption' => 'Configuration:');
  112.         foreach ($keys as $key) {
  113.             $type = $this->config->getType($key);
  114.             $value = $this->config->get($key, @$params[0]);
  115.             if ($type == 'password' && $value) {
  116.                 $value = '********';
  117.             }
  118.             if ($value === false) {
  119.                 $value = 'false';
  120.             } elseif ($value === true) {
  121.                 $value = 'true';
  122.             }
  123.             $data['data'][$this->config->getGroup($key)][] = array($this->config->getPrompt($key) , $key, $value);
  124.         }
  125.         $this->ui->outputData($data, $command);
  126.         return true;
  127.     }
  128.  
  129.     // }}}
  130.     // {{{ doConfigGet()
  131.  
  132.     function doConfigGet($command, $options, $params)
  133.     {
  134.         // $params[0] -> the parameter
  135.         // $params[1] -> the layer
  136.         if ($error = $this->_checkLayer(@$params[1])) {
  137.             return $this->raiseError($error);
  138.         }
  139.         if (sizeof($params) < 1 || sizeof($params) > 2) {
  140.             return $this->raiseError("config-get expects 1 or 2 parameters");
  141.         } elseif (sizeof($params) == 1) {
  142.             $this->ui->outputData("$params[0]=" . $this->config->get($params[0]), $command);
  143.         } else {
  144.             $data = "$params[1].$params[0]=" .$this->config->get($params[0], $params[1]);
  145.             $this->ui->outputData($data, $command);
  146.         }
  147.         return true;
  148.     }
  149.  
  150.     // }}}
  151.     // {{{ doConfigSet()
  152.  
  153.     function doConfigSet($command, $options, $params)
  154.     {
  155.         // $param[0] -> a parameter to set
  156.         // $param[1] -> the value for the parameter
  157.         // $param[2] -> the layer
  158.         $failmsg = '';
  159.         if (sizeof($params) < 2 || sizeof($params) > 3) {
  160.             $failmsg .= "config-set expects 2 or 3 parameters";
  161.             return PEAR::raiseError($failmsg);
  162.         }
  163.         if ($error = $this->_checkLayer(@$params[2])) {
  164.             $failmsg .= $error;
  165.             return PEAR::raiseError($failmsg);
  166.         }
  167.         if (!call_user_func_array(array(&$this->config, 'set'), $params))
  168.         {
  169.             $failmsg = "config-set (" . implode(", ", $params) . ") failed";
  170.         } else {
  171.             $this->config->store();
  172.         }
  173.         if ($failmsg) {
  174.             return $this->raiseError($failmsg);
  175.         }
  176.         return true;
  177.     }
  178.  
  179.     // }}}
  180.     // {{{ doConfigHelp()
  181.  
  182.     function doConfigHelp($command, $options, $params)
  183.     {
  184.         if (empty($params)) {
  185.             $params = $this->config->getKeys();
  186.         }
  187.         $data['caption']  = "Config help" . ((count($params) == 1) ? " for $params[0]" : '');
  188.         $data['headline'] = array('Name', 'Type', 'Description');
  189.         $data['border']   = true;
  190.         foreach ($params as $name) {
  191.             $type = $this->config->getType($name);
  192.             $docs = $this->config->getDocs($name);
  193.             if ($type == 'set') {
  194.                 $docs = rtrim($docs) . "\nValid set: " .
  195.                     implode(' ', $this->config->getSetValues($name));
  196.             }
  197.             $data['data'][] = array($name, $type, $docs);
  198.         }
  199.         $this->ui->outputData($data, $command);
  200.     }
  201.  
  202.     // }}}
  203.     // {{{ _checkLayer()
  204.  
  205.     /**
  206.      * Checks if a layer is defined or not
  207.      *
  208.      * @param string $layer The layer to search for
  209.      * @return mixed False on no error or the error message
  210.      */
  211.     function _checkLayer($layer = null)
  212.     {
  213.         if (!empty($layer) && $layer != 'default') {
  214.             $layers = $this->config->getLayers();
  215.             if (!in_array($layer, $layers)) {
  216.                 return " only the layers: \"" . implode('" or "', $layers) . "\" are supported";
  217.             }
  218.         }
  219.         return false;
  220.     }
  221.  
  222.     // }}}
  223. }
  224.  
  225. ?>